# -*- coding: utf-8 -*-


import json
from apps.common.base import BaseHandler


class SystemhealthHandler(BaseHandler):
    '''
    状态上报 /systemhealth
    '''

    async def post(self, *args, **kwargs):

        sn = self.get_argument('sn', default='')
        pingtime = self.get_argument('pingtime', default='')
        diskusage = self.get_argument('diskusage', default='')
        area = self.get_argument('area', default='')
        freemem = self.get_argument('freemem', default='')
        cpuidle = self.get_argument('cpuidle', default='')
        ioutil = self.get_argument('ioutil', default='')

        if not sn:
            return self.render_json(-1, 'sn null')

        # sn = '未知'
        if not pingtime:
            pingtime = '未知'
        if not diskusage:
            diskusage = '未知'
        if not area:
            area = '未知'
        if not freemem:
            freemem = '未知'
        if not cpuidle:
            cpuidle = '未知'
        if not ioutil:
            ioutil = '未知'
        data = {
            'sn': sn,
            'pingtime': pingtime,
            'diskusage': diskusage,
            'area': area,
            'freemem': freemem,
            'cpuidle': cpuidle,
            'ioutil': ioutil
        }

        cache = self.cache_action.rptsys().set(sn, json.dumps(data))

        if cache:
            return self.render_json(0, 'ok')
        else:
            return self.render_json(-1, 'error')
